<?php

@session_start();

require 'conf.php';

function listusers($sessiontoken)
{ 
    global $MW_host;
    $fields = array('ADMIN_LIST_USERS' => '',
                    'SESSION' => $sessiontoken);

    $fields = http_build_query($fields);

    $h = curl_init($MW_host); 
    curl_setopt($h, CURLOPT_POST, 1);
    curl_setopt($h, CURLOPT_POSTFIELDS, $fields); 
    curl_setopt($h, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($h, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($fields))); 

    $res = curl_exec($h);

    if(!mb_check_encoding($res, 'UTF-8'))
        $res = utf8_encode($res);
    $data = json_decode($res);
    return $data;
}

function haspic($session, $username)
{ 
    global $MW_host;
    $fields = array('USR_HAS_PIC' => '',
                    'SESSION' => $session,
                    'USERNAME' => $username);

    $fields = http_build_query($fields);

    $h = curl_init($MW_host); 
    curl_setopt($h, CURLOPT_POST, 1);
    curl_setopt($h, CURLOPT_POSTFIELDS, $fields); 
    curl_setopt($h, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($h, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($fields))); 

    $res = curl_exec($h);
    $data = json_decode($res);
    return $data;
}

if ($_SESSION['username'] != 'admin') {
    echo '<html><head><meta charset="utf-8"></head><body><h1><font color="red"><blink>Du är inte administratör. Denna incident har loggats</blink></font></h1></body></html>';
    exit();
}

$users = listusers($_SESSION['sessiontoken']);
if ($users === null) {
    echo 'Ett fel uppstod i komunikationen med mellan-servern';
    exit();
} else if (isset($users->error)) {
    echo $users->error;
    echo '<br><a href="/">Tillbaka</a>';
    exit();
}

?><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel='stylesheet' type='text/css' href='css/admin.css'>
<script type='text/javascript' src='js/jquery/jquery-1.5.2.min.js'></script>
<script type='text/javascript'>
    $(document).ready(function() {
        $('#del').submit(function() {
            var a = $('#del').serialize();
            $.post('delete_user.php', a, function(data) {
                var out = eval('(' + data + ')');
                if (out.error) {
                        $('#msg').html('<b><font color="red">Fel: ' + out.error + '</font></b>').hide().delay(200).fadeIn();
                    } else {
                        $('#msg').html('<b><font color="green">Användare borttagen.</font></b>').fadeIn("slow");
                        setTimeout(function() { window.location = '/admin.php'; }, 2000);
                    } 
            });
            

            return false;
        });
    });
</script>
</head>
<body>
<a href="/">Tillbaka</a>
<h1>Användare</h1>
<div id="container">
<form id="del" action="delete_user.php" method="POST">
<table>
    <tr><th>Uid</th><th>Användarnamn</th><th>Namn</th><th>Bild</th><th>Ta bort</th></tr>
    <? foreach ($users as $user): ?> 
    <tr>
        <td><?= $user->uid ?></td>    
        <td><?= $user->uname ?></td>
        <td><?= $user->name ?></td>
        <td>
        
        <?
            $data = haspic($_SESSION['sessiontoken'], $user->uname);
            $haspic = $data->haspic == true ? true : false;
            
            if ($haspic): ?>
            
            <img src="image.php?username=<?= $user->uname ?>" width=100 height=100>
            
            <? else: ?>
                Ingen bild
            <? endif; ?>
        
        <td><input type="radio" name="uid" value="<?= $user->uid ?>"></td>
    </tr>
    <? endforeach; ?>
</table>
<span id="button"><input id="delete_user_button" type="submit" value="Radera användare"><br><div id="msg" style="display:none"></div></span>
</form>
</div>
</body>
</html>
